package com.hzp.net.netty.client.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.Charset;

/**
 * netty server 通用处理器
 */
@Slf4j
public class NettyClientInHandlerFilter extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRegistered(ChannelHandlerContext ctx){
        log.info("NettyClientInHandlerFilter channelRegistered");
        ctx.fireChannelRegistered();
    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx){
        log.info("NettyClientInHandlerFilter channelUnregistered");
        ctx.fireChannelUnregistered();
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx){
        log.info("NettyClientInHandlerFilter channelActive");
//        ctx.fireChannelActive();
        ctx.write("hello".getBytes(Charset.defaultCharset()));
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx){
        log.info("NettyClientInHandlerFilter channelInactive");
        ctx.fireChannelInactive();
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg){
        log.info("NettyClientInHandlerFilter channelRead");
        ctx.fireChannelRead(msg);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx){
        log.info("NettyClientInHandlerFilter channelReadComplete");
        ctx.fireChannelReadComplete();
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt){
        log.info("NettyClientInHandlerFilter userEventTriggered");
        ctx.fireUserEventTriggered(evt);
    }
    @Override
    public void channelWritabilityChanged(ChannelHandlerContext ctx){
        log.info("NettyClientInHandlerFilter channelWritabilityChanged");
        ctx.fireChannelWritabilityChanged();
    }
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause){
        log.info("NettyClientInHandlerFilter exceptionCaught");
        ctx.fireExceptionCaught(cause);
    }

}
